package filter;

import utils.CookieUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.*;
import java.io.IOException;
@WebFilter(urlPatterns = {"/user/*","/JSP/*","/productCart.do","/c.do","/p.do"})
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession();
        Object username = session.getAttribute("username");
        if (username==null){
            response.sendRedirect(request.getContextPath() + "/login.jsp");
        }else {
            filterChain.doFilter(request,response);
        }
    }
}
